package com.example.springboot.mail;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * 邮件的配置
 *
 * @Author:ChenZhangKun
 * @Date: 2020/10/27 21:07
 */
@RestController
public class SendMail {
    @Autowired
    private JavaMailSenderImpl javaMailSender;

    /**
     * 简单邮件发送
     */
    @GetMapping("/mail")
    public void fun_9() {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setSubject("开会");
        simpleMailMessage.setText("今晚八点开会");
        simpleMailMessage.setFrom("1052031630@qq.com");
        simpleMailMessage.setTo("528402297@qq.com");
        javaMailSender.send(simpleMailMessage);
    }

    @GetMapping("/send/pdf")
    public String send() throws MessagingException {
        // 创建一个复杂的消息邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        // 邮件设置
        helper.setSubject("开会");
        helper.setText("<b style='color:red'>今天开会</b>", true);
        helper.setFrom("1052031630@qq.com");
        helper.setTo("528402297@qq.com");
        // 上传文件
        helper.addAttachment("day26--linux入门.pdf", new File("F:\\java资料\\day26--linux入门.pdf"));
        javaMailSender.send(mimeMessage);
        return "发送成功";
    }
}
